package com.cszy.ewmxns.ext

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import qrrspeeelild.rqrc0ode.andro0ild.R


/**
 * 启动Activity
 */
fun AppCompatActivity.launch(clazz: Class<*>) {
    startActivity(Intent(this, clazz))
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
}

fun Fragment.launch(clazz: Class<*>) {
    startActivity(Intent(context, clazz))
    (context as Activity).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
}

/**
 * 启动Activity
 */
fun Context.launch(clazz: Class<*>) {
    val intent = Intent(this, clazz)
    intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
}

/**
 * 启动Activity，携Bundle
 */
fun AppCompatActivity.launch(bundle: Bundle, clazz: Class<*>) {
    val intent = Intent(this, clazz)
    intent.putExtras(bundle)
    startActivity(intent)
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
}

fun Fragment.launch(bundle: Bundle, clazz: Class<*>) {
    val intent = Intent(context, clazz)
    intent.putExtras(bundle)
    startActivity(intent)
    (context as Activity).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
}

/**
 * 传递intent,启动Activity
 */
fun Context.launch(bundle: Bundle, clazz: Class<*>) {
    val intent = Intent(this, clazz)
    intent.putExtras(bundle)
    intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
}


/**
 * 检查是否finish或者否被销毁
 */
fun AppCompatActivity.isFinish(): Boolean {
    return isFinishing || isDestroyed
}

/**
 * 显示Toast
 */
fun AppCompatActivity.toast(message: CharSequence) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

fun Fragment.toast(message: CharSequence) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}

/**
 * 显示Toast
 */
fun Context.toast(message: CharSequence) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

